﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Linq;
using System.Windows.Data;

namespace BezierSegmentDemo
{
    [TemplatePart(Name = "PART_Canvas", Type = typeof(Canvas))]
    [TemplatePart(Name = "PART_Figure", Type = typeof(PathFigure))]
    public class CustomFigure : Control
    {
        private List<ThumbPoint> thumbPoints = new List<ThumbPoint>();

        private Canvas PART_Canvas;

        private PathFigure PART_Figure;

        #region StartPoint
        public Point StartPoint
        {
            get { return (Point)GetValue(StartPointProperty); }
            set { SetValue(StartPointProperty, value); }
        }

        // Using a DependencyProperty as the backing store for StartPoint.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty StartPointProperty =
            DependencyProperty.Register("StartPoint", typeof(Point), typeof(CustomFigure), new PropertyMetadata(new Point()));
        #endregion

        #region Points
        public PathSegmentCollection Points
        {
            get { return (PathSegmentCollection)GetValue(PointsProperty); }
            set { SetValue(PointsProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Points.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty PointsProperty =
            DependencyProperty.Register("Points", typeof(PathSegmentCollection), typeof(CustomFigure), new PropertyMetadata(new PathSegmentCollection()));
        #endregion

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            this.PART_Canvas = (Canvas)this.Template.FindName("PART_Canvas", this);
            this.PART_Figure = (PathFigure)this.Template.FindName("PART_Figure", this);
        }

        #region AddPoint
        public void AddPoint(Point p)
        {
            if (this.PART_Canvas == null)
            {
                return;
            }

            if (this.thumbPoints.Count == 0)
            {
                this.StartPoint = p;
                this.AddStartPoint();
            }
            else
            {
                LineSegment line = new LineSegment(p, true);
                this.PART_Figure.Segments.Add(line);
                this.AddThumbPoint(line);
            }
        }

        private void AddThumbPoint(LineSegment p)
        {
            ThumbPoint thumb = new BezierSegmentDemo.ThumbPoint();
            Binding binding = new Binding();
            binding.Source = p;
            binding.Mode = BindingMode.TwoWay;
            binding.Path = new PropertyPath("Point");
            thumb.SetBinding(ThumbPoint.PointProperty, binding);
            this.PART_Canvas.Children.Add(thumb);
            this.thumbPoints.Add(thumb);
        }

        private void AddStartPoint()
        {
            ThumbPoint thumb = new BezierSegmentDemo.ThumbPoint();
            Binding binding = new Binding();
            binding.Source = this.PART_Figure;
            binding.Mode = BindingMode.TwoWay;
            binding.Path = new PropertyPath("StartPoint");
            thumb.SetBinding(ThumbPoint.PointProperty, binding);
            this.PART_Canvas.Children.Add(thumb);
            this.thumbPoints.Add(thumb);
        }
        #endregion
    }
}
